import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/login/index.vue'),
            meta: {
                isSinglePage: true // 是否是单独的页面（不包含侧边栏，头部组件）
            }
        },
        {
            path: '/',
            name: 'layout',
            redirect: 'welcome',
            component: () => import('../views/Layout/index.vue'),
            // 嵌套路由
            children: [
                {
                    path: 'welcome',
                    // name: 'welcome',
                    component: () => import('../views/welcome/index.vue'), // 路由懒加载
                    meta: {
                        title: '首页'
                    }
                },
                {
                    path: '/about',
                    name: 'about',
                    component: () => import('../views/About.vue'), // 路由懒加载
                    meta: {
                        title: '关于'
                    }
                },
                {
                    path: '/network-one',
                    component: () => import('../views/networdOne/index.vue'),
                    redirect: '/network-one/import',
                    meta: {
                        title: '网站一'
                    },
                    children: [{
                        path: 'import',
                        component: () => import('../views/networdOne/index.vue'), // 路由懒加载
                        meta: {
                            title: '文件导入'
                        },
                        children: [{
                            path: 'list',
                            component: () => import('../views/networdOne/index.vue'), // 路由懒加载
                            meta: {
                                title: '数据列表页'
                            }
                        },]
                    },
                    {
                        path: 'newdata',
                        component: () => import('../views/networdOne/index.vue'), // 路由懒加载
                        meta: {
                            title: '数据发布'
                        }
                    },]
                },
                {
                    path: '/network-two',
                    component: () => import('../views/networdTwo/index.vue'),
                    redirect: '/network-two/import2',
                    meta: {
                        title: '网站二'
                    },
                    children: [{
                        path: 'import2',
                        component: () => import('../views/networdTwo/index.vue'), // 路由懒加载
                        meta: {
                            title: '文件导入'
                        },
                        children: [{
                            path: 'list',
                            component: () => import('../views/networdTwo/index.vue'), // 路由懒加载
                            meta: {
                                title: '数据列表页'
                            }
                        },]
                    },
                    {
                        path: 'newdata2',
                        component: () => import('../views/networdTwo/index.vue'), // 路由懒加载
                        meta: {
                            title: '数据发布'
                        }
                    }]
                },
                {
                    path: '/total',
                    name: 'total',
                    component: () => import('../views/Total/index.vue'), // 路由懒加载
                    meta: {
                        title: '全局弹窗组件'
                    }
                },
            ]
        },
        {
            path: '/:pathMatch(.*)',
            name: '404',
            component: () => import('../views/404/index.vue')
        }
    ]
})

router.beforeEach((to, from, next) => {
    const isLogin = JSON.parse(decodeURIComponent(sessionStorage.getItem('login') as string))
    // 用户未登录，跳转到登录页
    if (to.name !== 'login' && !isLogin) {
        next({ name: 'login' })
    } else if (isLogin && to.name === 'login') {
        // 用户已登录，禁止跳转到登录页
        next(false)
    }
    else next()
})

export default router